import { nmAppErrorLog } from '@app-nextmesh/common/utils';
import { showLoading } from '@app-nextmesh/components';
import { documentV2 } from '@app-nextmesh/domain';
import { useTasksStore } from '@app-nextmesh/store/tasks';
import AlertDialog from '../AlertDialog';

interface IProps {
  uuid: string;
  onOk: () => void;
  onClose: () => void;
}
export default function TaskAlertDialog({ uuid, onOk, onClose }: IProps) {
  const { tasks, setTasks } = useTasksStore();
  const doDelete = async () => {
    const hide = showLoading();
    try {
      await documentV2.deleteTask(uuid);
      setTasks(tasks.filter((task) => task.uuid !== uuid));
      onOk();
      onClose();
    } catch (error) {
      nmAppErrorLog('SimulationAlertDialog', error);
    } finally {
      hide();
    }
  };
  return (
    <AlertDialog
      alert='确定删除这个任务吗？删除求解将删除所有相关数据，无法恢复。'
      primaryTitle='确定删除'
      onPrimaryClick={doDelete}
      onSecondaryClick={onClose}
    />
  );
}
